package com.xn.app.muying.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xn.app.muying.R;
import com.xn.app.muying.adpter.DoctorListAdapter;
import com.xn.app.muying.bean.DoctorListBean;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.HttpUtil;
import com.xn.app.muying.util.MyAlertDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/2.
 */
public class DoctorListActivity extends AppCompatActivity implements HttpUtil.OnGetDataListener, View.OnClickListener {

    private ListView lv;
    private Handler handler = new Handler();
    private DoctorListAdapter doctorListAdapter;
    private TextView title;
    private ImageView back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_list);
        lv = (ListView) findViewById(R.id.activity_doctor_list_listview);
        title = (TextView) findViewById(R.id.activity_doctor_list_title);
        back = (ImageView) findViewById(R.id.activity_doctor_list_back);

        back.setOnClickListener(this);
        Intent intent = getIntent();
        String doctorlist_id = intent.getStringExtra("doctorlist_id");
        HttpUtil.getData(APIMethod.Home_DoctorList(doctorlist_id),handler,this);
        doctorListAdapter = new DoctorListAdapter(DoctorListActivity.this,handler);
        lv.setAdapter(doctorListAdapter);

    }

    @Override
    public void onJsonData(String data) {
        try {
            JSONObject jsonObject = new JSONObject(data);
            final String subDepartmentName = jsonObject.optString("subDepartmentName");
            JSONArray items = jsonObject.getJSONArray("items");
            ArrayList<DoctorListBean> list = new ArrayList<>();
            for (int i = 0; i < items.length(); i++) {
                JSONObject jsonObject1 = items.getJSONObject(i);
                String name = jsonObject1.optString("name");
                String photoUrl = jsonObject1.optString("photoUrl");
                String professionalTitle = jsonObject1.optString("professionalTitle");
                String primaryDepartmentName = jsonObject1.optString("primaryDepartmentName");
                String primaryHospitalName = jsonObject1.optString("primaryHospitalName");
                String primaryHospitalLevel = jsonObject1.optString("primaryHospitalLevel");
                String specialty = jsonObject1.optString("specialty");
                String id = jsonObject1.optString("id");
                DoctorListBean doctorListBean = new DoctorListBean();
                doctorListBean.setName(name);
                doctorListBean.setPhotoUrl(photoUrl);
                doctorListBean.setProfessionalTitle(professionalTitle);
                doctorListBean.setPrimaryDepartmentName(primaryDepartmentName);
                doctorListBean.setPrimaryHospitalName(primaryHospitalName);
                doctorListBean.setPrimaryHospitalLevel(primaryHospitalLevel);
                doctorListBean.setSpecialty(specialty);
                doctorListBean.setId(id);
                list.add(doctorListBean);

            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    title.setText(subDepartmentName);
                }
            });
            doctorListAdapter.getData(list);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.activity_doctor_list_back:
                finish();
                break;
        }
    }
}
